#include <iostream>
using namespace std;

#include "RICHPMTDetector.hh"
#include "RICHHitsCollection.hh"
#include "RICHHit.hh"
#include "G4SDManager.hh"

RICHPMTDetector::RICHPMTDetector(G4String name) :
		G4VSensitiveDetector(name) {
	hitsCollection = 0;
	collectionName.insert("1");
}

RICHPMTDetector::~RICHPMTDetector() {
	;
}

void RICHPMTDetector::Initialize(G4HCofThisEvent* hcc) {
	hitsCollection = new RICHHitsCollection(SensitiveDetectorName,
			collectionName[0]);

	G4SDManager* sdMan = G4SDManager::GetSDMpointer();

	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID, hitsCollection);
}

void RICHPMTDetector::EndOfEvent(G4HCofThisEvent*) {
	;
}

G4bool RICHPMTDetector::ProcessHits(G4Step *step, G4TouchableHistory*) {
	G4Track* track = step->GetTrack();
	double time = track->GetGlobalTime();
	G4ThreeVector pos = track->GetPosition();
	if (step->GetTotalEnergyDeposit() <= 0)
		return false;
	if (fabs(pos.getZ()) > mm || fabs(pos.getX()) > 5 * cm
			|| fabs(pos.getY()) > 5 * cm)
		return false;

	RICHHit* hit = new RICHHit(time, pos);
	hitsCollection->Insert(hit);
//	cout << hitsCollection->GetSize() << endl;
	cout << "{" << pos.getX() / cm << ", " << pos.getY() / cm << "}," << endl;

	return true;
}
